package com.hucii.hc_connectivity;

import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.view.TextureRegistry;

/**
 * HcHcConnectivityPlugin
 */
public class HcConnectivityPlugin implements FlutterPlugin, ActivityAware {

    private MethodChannel methodChannel;
    private EventChannel eventChannel;
    private Activity hcActivity;
    private Connectivity connectivity;

    /**
     * Plugin registration.
     */
    @SuppressWarnings("deprecation")
    public static void registerWith(io.flutter.plugin.common.PluginRegistry.Registrar registrar) {

        HcConnectivityPlugin plugin = new HcConnectivityPlugin();
        plugin.setupChannels(registrar.messenger(), registrar.context());
        plugin.initWithActivity(registrar.activity());
        plugin.connectivity.setPermissionsRegistry(registrar::addRequestPermissionsResultListener);
        registrar.addActivityResultListener(plugin.connectivity.resultListener);

    }

    @Override
    public void onAttachedToEngine(FlutterPluginBinding binding) {
        setupChannels(binding.getBinaryMessenger(), binding.getApplicationContext());
    }

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        // init with activity
        initWithActivity(binding.getActivity());
        connectivity.setPermissionsRegistry(binding::addRequestPermissionsResultListener);
        binding.addActivityResultListener(connectivity.resultListener);
    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {
        // set activity to null
        hcActivity = null;
    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
        // init with activity
        initWithActivity(binding.getActivity());
        binding.addActivityResultListener(connectivity.resultListener);
    }

    @Override
    public void onDetachedFromActivity() {
        // set activity to null
        hcActivity = null;
    }

    @Override
    public void onDetachedFromEngine(FlutterPluginBinding binding) {
        teardownChannels();
    }

    private void initWithActivity(Activity activity) {
        hcActivity = activity;
        connectivity.setActivity(hcActivity);
    }

    private void setupChannels(BinaryMessenger messenger, Context context) {
        methodChannel = new MethodChannel(messenger, "plugins.hucii.com/connectivity");
        eventChannel = new EventChannel(messenger, "plugins.hucii.com/connectivity_status");
        final ConnectivityManager connectivityManager =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        final WifiManager wifiManager =
                (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);//获取服务
        locationManager.getProviders(true);
        connectivity = new Connectivity(connectivityManager, wifiManager, context, new ConnectPermissions());

        ConnectivityMethodChannelHandler methodChannelHandler =
                new ConnectivityMethodChannelHandler(connectivity);
        ConnectivityBroadcastReceiver receiver =
                new ConnectivityBroadcastReceiver(context, connectivity);

        methodChannel.setMethodCallHandler(methodChannelHandler);
        eventChannel.setStreamHandler(receiver);
    }

    private void teardownChannels() {
        methodChannel.setMethodCallHandler(null);
        eventChannel.setStreamHandler(null);
        methodChannel = null;
        eventChannel = null;
    }
}
